<template>
	<view class="mainbgc">
		<header-top :messageInfo="{typeFlag:true,message:'采购退货单统计明细',liner:false}">
			<view class="rightmsg" slot="rightmsg">
				<view class="rightmsg_content flexleft" @click="startPrint(35)">
					<image class="img" src="../../../static/image/system/my2.png" mode=""></image>
				</view>
			</view>
		</header-top>
		<view class="mainpadding_top ffffff">
			<view class="header flexbetween">
				<view class="headerchild" style="flex: 1;">商品名称</view>
				<view class="headerchild headercenter" style="flex: 1;">规格</view>
				<view class="headerchild headercenter" style="flex: 0.7;">数量</view>
				<view class="headerchild headerright" style="flex: 0.7;">金额</view>
			</view>
			<view class="content_child " v-for="item in dataAll.data" :key="item.id">
				<view class="flexbetween">
					<view class="contentchild" style="flex: 1;">{{item.goods_info.name}}</view>
					<view class="contentchild headercenter" style="flex: 1;" v-show="item.difference_type==1">
						{{item.goods_info.biggoodsunit.name}}</view>
					<view class="contentchild headercenter" style="flex: 1;" v-show="item.difference_type==2">
						{{item.goods_info.middlegoodsunit.name}}</view>
					<view class="contentchild headercenter" style="flex: 1;" v-show="item.difference_type==3">
						{{item.goods_info.smallgoodsunit.name}}</view>
					<view class="contentchild headercenter" style="flex: 0.7;">*{{item.all_num}}</view>
					<view class="contentchild miancolor headerright" style="flex: 0.7;">￥{{item.all_total_price}}</view>
				</view>
			</view>
		</view>
		<!-- <view class="mainpadding_top ffffff strongtext flexbetween">
			<view class="">配送方式</view>
			<view class="">自提</view>
		</view>
		
		<view class="mainpadding_top ffffff strongtext flexbetween">
			<view class="">是否开具发票</view>
			<view class="">自提</view>
		</view>
		<view class="mainpadding_top ffffff strongtext flexbetween">
			<view class="">优惠</view>
			<view class="">19929元</view>
		</view>
		<view class="mainpadding_top ffffff strongtext flexbetween">
			<view class="">运费</view>
			<view class="">19929元</view>
		</view>
		<view class="mainpadding_top ffffff strongtext">
			<view class="">备注</view>
			<view class="">备注</view>
		</view> -->
		<view class="mainpadding">
			<view class="flexleft margin_top">
				<view class="contentchild">品种：{{dataAll.goods_zl_num}}种，</view>
				<view class="contentchild">数量：{{dataAll.goods_num}}件，</view>
				<view class="contentchild">客户：{{dataAll.kh_num}}个</view>
			</view>
			<view class="flexleft margin_top">
				<view class="contentchild">合计：</view>
				<view class="xiaolv">{{dataAll.total_all_total_price}}元</view>
			</view>
		</view>

		<moDal :message="{modalShow:modal.modalShow,title:modal.title}" @modalSure="modalSure"></moDal><!-- 模态框 -->
	</view>
</template>

<script>
	const httpRequest = require("@/common/httpRequest.js")
	const tsc = require("@/util/ble/tsc.js");
	const esc = require("@/util/ble/esc.js");
	const encode = require("@/util/ble/encoding.js");
	import {
		mapState
	} from 'vuex';
	export default {
		data() {
			return {
				arr: [],
				type: "", //1采购单，
				dataAll: {
					list: []
				},
				url: "",
				titletext: "",
				// 模态框
				modal: {
					modalShow: false,
					title: "",
				},
				// 打印机技术参数
				sendContent: "../../../static/image/news/boy.png",
				looptime: 0,
				currentTime: 1,
				lastData: 0,
				oneTimeData: 0,
				returnResult: "",
				canvasWidth: 180,
				canvasHeight: 180,
				imageSrc: '../../static/img/abc_ic_star_black_16dp.png',
				buffSize: [],
				buffIndex: 0,
				printNum: [],
				printNumIndex: 0,
				printerNum: 1,
				currentPrint: 1,
				isReceiptSend: false,
				isLabelSend: false,
				// 打印机技术结束
			}
		},
		onLoad(options) {
			let data = JSON.parse(decodeURIComponent(options.data));
			this.arr = data.arr
			this.type = data.type
			this.init()
			// 打印机
			let that = this;
			let {
				BLEInformation
			} = that.Bluetooth;
			// 打印机结束
		},
		// 打印机开始
		computed: mapState(['sysinfo', 'Bluetooth']),
		onReady() {
			let list = []
			let numList = []
			let j = 0
			for (let i = 20; i < 200; i += 10) {
				list[j] = i;
				j++
			}
			for (let i = 1; i < 10; i++) {
				numList[i - 1] = i
			}
			this.buffSize = list;
			this.oneTimeData = list[0];
			this.printNum = numList;
			this.printerNum = numList[0];
		},
		onUnload() {
			let that = this;
			let {
				BLEInformation
			} = that.Bluetooth;
		},
		// 打印机结束
		onShow() {
			// 打印机开始
			let that = this;
			let width;
			let height;
			uni.getImageInfo({
				src: that.imageSrc,
				success(res) {
					console.log(res.width)
					console.log(res.height)
					width = res.width
					height = res.height
					that.canvasWidth = res.width;
					that.canvasHeight = res.height;
				}
			})
			const ctx = uni.createCanvasContext("edit_area_canvas", this);
			ctx.drawImage(this.imageSrc, 0, 0, width, height);
			ctx.draw();
			// 打印机结束
		},
		methods: {
			// 打印机开始
			receiptTest() {
				// this.selectshow = false
				var that = this;
				var canvasWidth = that.canvasWidth
				var canvasHeight = that.canvasHeight
				var command = esc.jpPrinter.createNew()
				command.init()
				// 标题
				command.bold(1); //加粗
				command.setFontSize(16); //字体大小
				command.setSelectJustification(1) //居中0左对齐1居中对齐2右对齐
				command.rowSpace(100);
				command.setText("采购退货单统计明细");
				command.setPrint();
				command.rowSpace(60); //行间距
				command.bold(0); //取消加粗
				command.setFontSize(0); //正常字体
				// 内容
				command.bold(0); //加粗
				command.setSelectJustification(0) //居中0左对齐1居中对齐2右对齐
				command.setText("打印时间：" + httpRequest.timereturn(Number(new Date())));
				command.setPrint();
				// 表格
				command.rowSpace(80); //间距
				command.bold(5); //加粗
				command.setText("商品名称");
				command.setAbsolutePrintPosition(270);
				command.setText("数量");
				command.setAbsolutePrintPosition(380)
				command.setText("金额");
				command.setPrint()
				command.bold(0); //加粗
				that.dataAll.data.forEach(item=>{
					command.setText(item.goods_info.name);
					command.setAbsolutePrintPosition(270);
					command.setText(item.all_num);
					command.setAbsolutePrintPosition(380);
					command.setText(Number(item.all_total_price));
					command.setPrint()
					if(item.difference_type==1){
						command.setText(item.goods_info.biggoodsunit.name);
					}else if(item.difference_type==2){
						command.setText(item.goods_info.middlegoodsunit.name);
					}else{
						command.setText(item.goods_info.smallgoodsunit.name);
					}
					command.setPrint()
				})
				command.bold(5); //加粗
				command.setText("品种：" + that.dataAll.goods_zl_num+"，数量："+that.dataAll.goods_num+"，客户："+that.dataAll.kh_num);
				command.setPrint()
				command.setText("合计："+that.dataAll.total_all_total_price);
				command.setPrint()
				command.setText("签字：");
				command.setPrint()
				command.setSelectJustification(1) //居中0左对齐1居中对齐2右对齐
				command.setText("询博技术支持");
				command.setPrintAndFeedRow(3);
				that.isReceiptSend = true;
				that.prepareSend(command.getData());
			
			
			},
			
			//准备发送，根据每次发送字节数来处理分包数量
			prepareSend(buff) {
				console.log(buff);
				let that = this
				let time = that.oneTimeData
				let looptime = parseInt(buff.length / time);
				let lastData = parseInt(buff.length % time);
				console.log(looptime + "---" + lastData)
				this.looptime = looptime + 1;
				this.lastData = lastData;
				this.currentTime = 1;
				that.Send(buff)
			},
			//分包发送
			Send(buff) {
				let that = this
				let {
					currentTime,
					looptime: loopTime,
					lastData,
					oneTimeData: onTimeData,
					printerNum: printNum,
					currentPrint
				} = that;
				let buf;
				let dataView;
				if (currentTime < loopTime) {
					buf = new ArrayBuffer(onTimeData)
					dataView = new DataView(buf)
					for (var i = 0; i < onTimeData; ++i) {
						dataView.setUint8(i, buff[(currentTime - 1) * onTimeData + i])
					}
				} else {
					buf = new ArrayBuffer(lastData)
					dataView = new DataView(buf)
					for (var i = 0; i < lastData; ++i) {
						dataView.setUint8(i, buff[(currentTime - 1) * onTimeData + i])
					}
				}
				console.log("第" + currentTime + "次发送数据大小为：" + buf.byteLength)
				let {
					BLEInformation
				} = that.Bluetooth;
			
				plus.bluetooth.writeBLECharacteristicValue({
					deviceId: BLEInformation.deviceId,
					serviceId: BLEInformation.writeServiceId,
					characteristicId: BLEInformation.writeCharaterId,
					value: buf,
					success: function(res) {
						console.log(res)
					},
					fail: function(e) {
						console.log(e)
					},
					complete: function() {
						currentTime++
						if (currentTime <= loopTime) {
							that.currentTime = currentTime;
							that.Send(buff)
						} else {
							//  uni.showToast({
							// title: '已打印第' + currentPrint + '张',
							//  })
							if (currentPrint == printNum) {
								that.looptime = 0;
								that.lastData = 0;
								that.currentTime = 1;
								that.isReceiptSend = false;
								that.isLabelSend = false;
								that.currentPrint = 1;
							} else {
								currentPrint++;
								that.currentPrint = currentPrint;
								that.currentTime = 1;
								that.Send(buff)
							}
						}
					}
				})
			},
			// 打印机结束
			init() {
				httpRequest.request("/dhtapi/Procurethorder/tjProcurethOrder", 'GET', {
					ids: this.arr,
				}, false, false, true).then(res => {
					this.dataAll = res.data
				}).catch(err => {
					httpRequest.toast(err.msg);
				})
			},
			startPrint(qxpdid) {
				if (!httpRequest.qxpd(qxpdid, this.$store.state.userInfo.ability_rule_ids)) {
					return false
				}
				let that = this
				uni.showActionSheet({
					itemList: ['云打印机', '蓝牙打印机'],
					success: function(res) {
						// 选择云打印机走0
						if (res.tapIndex == 0) {
							that.modal.title = "请再次确认打印！"
							that.modalSure()
						}
						// 选择蓝牙打印机走1
						if (res.tapIndex == 1) {
							that.receiptTest()
						}
					},
					fail: function(res) {}
				});
			},
			modalSure() {
				this.modal.modalShow = false
				if (this.modal.title == "请再次确认打印！") {
					uni.showLoading({
						mask: true,
						title: "发送数据中"
					})
					httpRequest.request("/dhtapi/Procurethorder/dytjProcureOrder", 'GET', {
						ids: this.arr,
					}, false, false, true).then(res => {
						uni.hideLoading()
						httpRequest.toast(res.msg)
					}).catch(err => {
						httpRequest.toast(err.msg);
					})
				}
			},
		}
	}
</script>

<style>
	.img {
		width: 40rpx;
		height: 40rpx;
	}

	.dayinji {
		width: 40rpx;
		height: 40rpx;
		background-image: url("@/static/image/system/my2.png");
		background-size: 100% 100%;
	}

	.miancolor {
		color: #0BBDCB;
	}

	.totle {
		padding-bottom: 150rpx;
	}

	.margin_topx {
		margin-top: 10rpx;
	}

	.huisetext {
		font-size: 24rpx;
		font-family: PingFang SC-Medium, PingFang SC;
		font-weight: 500;
		color: #999999;
	}

	.lansetext {
		font-size: 24rpx;
		font-family: PingFang SC-Bold, PingFang SC;
		font-weight: bold;
		color: #999999;
	}

	.center {
		padding: 0 40rpx;
		margin: 30rpx auto;
		background-color: #fff;
	}

	.foot {
		padding: 0 40rpx;
		margin: 0 auto;
	}

	.headercenter {
		text-align: center;
	}

	.headerchild {
		font-size: 32rpx;
		font-weight: bold;
	}

	.headerbottom {
		font-size: 28rpx;
		font-weight: bold;
	}

	.content {
		padding-top: 30rpx;
	}

	.contentchild {
		font-size: 26rpx;
	}

	.headerright {
		text-align: right;
	}

	.content_child {
		margin: 30rpx 0 0 0;
	}
</style>